package com.ebt.m.fileExplore.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.fileExplore.model.ItemFile;

import java.util.ArrayList;

public class AdapterFileExplore extends BaseAdapter {
    private LayoutInflater inflater;
    private Bitmap folder;
    private Bitmap file;

    private ArrayList<ItemFile> items;

    public AdapterFileExplore(Context context, ArrayList<ItemFile> items) {
        this.items = items;
        folder = BitmapFactory.decodeResource(context.getResources(), R.drawable.folder);
        file = BitmapFactory.decodeResource(context.getResources(), R.drawable.file);
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemFile item = items.get(position);

        ViewHolder holder;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.view_file_explore_item, parent, false);
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.textView);
            holder.image = (ImageView) convertView.findViewById(R.id.imageView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(item.name);

        if (item.isFile()) {
            holder.image.setImageBitmap(file);
        } else {
            holder.image.setImageBitmap(folder);
        }

        return convertView;
    }

    private class ViewHolder {
        private TextView text;
        private ImageView image;
    }

}
